package com.competition.oss.controller;

import com.competition.commonutils.R;
import com.competition.oss.entity.Bill;
import com.competition.oss.entity.BillVo;
import com.competition.oss.entity.DayTimes;
import com.competition.oss.entity.Product;
import com.competition.oss.service.BillService;
import com.competition.oss.service.DayTimesService;
import com.competition.oss.service.OssService;
import com.competition.oss.utils.OcrUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import com.google.gson.Gson;

import java.io.IOException;
import java.util.*;

@RestController
@RequestMapping("/oss")
@CrossOrigin
@Api(description = "图片上传相关的接口")
public class OssController {

    @Autowired
    private OssService ossService;

    @Autowired
    private DayTimesService dayTimesService;

    @PostMapping("upload")
    @ApiOperation("上传图片，返回url,dataUuid和识别数据")
    public R uploadOssFile(MultipartFile file) throws IOException {  //上传文件
        String url = ossService.uploadFileAvatar(file);
        JSONObject ocrJSON = OcrUtils.getOcrJSON(file);
        Gson gson = new Gson();
        //图片识别扫码结果
        HashMap hashMap = gson.fromJson(ocrJSON.toString(), HashMap.class);
        //把结果转化为List集合
        return R.ok().data("url", url).data("ocrJSON", hashMap);
    }


    @PostMapping("getUploadInfo")
    @ApiOperation("上传图片，返回url和识别数据")
    public R getUploadInfo(MultipartFile file) throws Exception {
        //使用次数判断
        int excess = dayTimesService.updateTimes();
        if(excess==0){
            return R.error().data("msg","使用次数超时");
        }
        //图片上传
        String url = ossService.uploadFileAvatar(file);
        JSONObject ocrJSON = OcrUtils.getOcrJSON(file);
        //解析数据
        Gson gson = new Gson();
        //图片识别扫码结果
        HashMap hashMap = gson.fromJson(ocrJSON.toString(), HashMap.class);
        Object words_result = hashMap.get("words_result");
        System.out.println(words_result.toString());
        Map<Object, Object> map = (Map<Object, Object>) words_result;
        BillVo billVo = new BillVo();
        String invoiceNum = map.get("InvoiceNum").toString();
        //发票识别号
        billVo.setInvoiceNum(invoiceNum);
        //购买方名称
        billVo.setPurchaserName(map.get("PurchaserName").toString());
        //购买方纳税人识别号
        billVo.setPurchaserRegisterNum(map.get("PurchaserRegisterNum").toString());
        //购买方电话及地址
        billVo.setPurchaserAddress(map.get("PurchaserBank").toString());
        //购买方开户行及账号
        billVo.setPurchaserBank(map.get("PurchaserBank").toString());
        //销售方名称
        billVo.setSellerName(map.get("SellerName").toString());
        //销售方识别号
        billVo.setSellerRegisterNum(map.get("SellerRegisterNum").toString());
        //销售方地址及电话
        billVo.setSellerAddress(map.get("SellerAddress").toString());
        //销售方开户行及账户
        billVo.setSellerBank(map.get("SellerBank").toString());
        //总税额
        billVo.setTotalTax(map.get("TotalTax").toString());
        //总金额
        billVo.setAmountInFiguers(map.get("AmountInFiguers").toString());
        //收款人
        billVo.setPayee(map.get("Payee").toString());
        //复审
        billVo.setChecker(map.get("Checker").toString());
        //开票人
        billVo.setNoteDrawer(map.get("NoteDrawer").toString());
        billVo.setStatu(0);
        //商品集合
        List<Product> productList = new ArrayList<>();
        //服务名称
        List<Map<Object, Object>> CommodityNameList = (List<Map<Object, Object>>) map.get("CommodityName");
        int CommodityNameListLen = CommodityNameList.size();
        System.out.println(CommodityNameList.toString());
        //服务数量
        List<Map<Object, Object>> CommodityNumList = (List<Map<Object, Object>>) map.get("CommodityNum");
        int CommodityNumListLen = CommodityNumList.size();
        System.out.println(CommodityNumList.toString());
        //单价
        List<Map<Object, Object>> CommodityPriceList = (List<Map<Object, Object>>) map.get("CommodityPrice");
        int CommodityPriceListLen = CommodityPriceList.size();
        System.out.println(CommodityPriceList.toString());
        //税额
        List<Map<Object, Object>> CommodityAmountList = (List<Map<Object, Object>>) map.get("CommodityAmount");
        int CommodityAmountListLen = CommodityAmountList.size();
        System.out.println(CommodityAmountList.toString());
        //税率
        List<Map<Object, Object>> CommodityTaxRateList = (List<Map<Object, Object>>) map.get("CommodityTaxRate");
        int CommodityTaxRateListLen = CommodityTaxRateList.size();
        List<Map<Object, Object>> CommodityTaxList = (List<Map<Object, Object>>) map.get("CommodityTax");
        int CommodityTaxListLen = CommodityTaxList.size();
        //服务名称
        for (int i = 0; i < CommodityNameListLen; i++) {
            Product product = new Product();
            product.setCommodityName((String) CommodityNameList.get(i).get("word"));
            productList.add(product);
            System.out.println((String) CommodityNameList.get(i).get("word"));
        }
        //服务数量
        for (int i = 0; i < CommodityNumListLen; i++) {
            Product product = productList.get(i);
            product.setCommodityNum(CommodityNumList.get(i).get("word").toString());
            productList.set(i, product);
        }
        //单价
        for (int i = 0; i < CommodityPriceListLen; i++) {
            Product product = productList.get(i);
            product.setCommodityPrice((String) CommodityPriceList.get(i).get("word"));
            productList.set(i, product);
            System.out.println((String) CommodityPriceList.get(i).get("word"));
        }
        //该商品总价格
        for (int i = 0; i < CommodityAmountListLen; i++) {
            Product product = productList.get(i);
            product.setCommodityAmount((String) CommodityAmountList.get(i).get("word"));
            productList.set(i, product);
            System.out.println(CommodityAmountList.get(i));
        }
        //税率
        for (int i = 0; i < CommodityTaxRateListLen; i++) {
            Product product = productList.get(i);
            product.setCommodityTaxRate((String) CommodityTaxRateList.get(i).get("word"));
            productList.set(i, product);
            System.out.println(CommodityTaxRateList.get(i));
        }
        //税额
        for (int i = 0; i < CommodityTaxListLen; i++) {
            Product product = productList.get(i);
            product.setCommodityTax((String) CommodityTaxList.get(i).get("word"));
            productList.set(i, product);
            System.out.println(CommodityTaxList.get(i));
        }
        billVo.setProductList(productList);
        //把结果转化为List集合
        return R.ok().data("url", url).data("ocrJSON", hashMap).data("billVo", billVo);
    }


    @PostMapping("deletefile")
    @ApiOperation("根据图片路径删除图片")
    public R deleteFile(String url) {
        System.out.println(url);
        ossService.deleteFile(url);
        return R.ok();
    }

}
